import { httpJsonp } from "./http";
import { getData, postData } from "./server";
export const checkMessageCode = (params: { phone: string | number; messageCode: string | number }) => {
	return getData("/checkMessageCode.json", params);
};

export const getMessageCode = (params: { phone: string | number }) => {
	return getData("/getMessageCode.json", params);
};

export const submitForm = (data: {
	phone: string | number;
	messageCode: string | number;
	data: {
		province: string;
		provinceCode: string;

		city: string;
		cityCode: string;

		district: string;
		districtCode: string;

		street: string;
		streetCode: string;

		address: string;

		companyName: string;
		socialCreditCode: string;

		linkManName: string;
		mobilePhone: string | number;
		countryPhoneCode: string;

		//
		local: string;
		origin: string;
		willingnessKeys: string[];
		pageUrl: string;
		joinSource: string;
	};
	pcsrf: string;
}) => {
	// const covertFormData = (obj: any) => {
	// 	const formData = new FormData();
	// 	Object.keys(obj).map((key: any) => {
	// 		formData.append(key, obj[key]);
	// 	});
	// 	return formData;
	// };
	// const formData = covertFormData(data);
	return postData("/potential/submitAndVerifyWithWeCom.do", data, {
		"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
	});
};

let commonFormUrl = "/front/getJsonComponent.json";
if (process.env.NODE_ENV === "production") {
	commonFormUrl = "https://widget.1688.com" + commonFormUrl;
}
export const getAddressVoData = (params: { reginId: number | string }) => {
	return httpJsonp(commonFormUrl, {
		namespace: "getChinaRegionList",
		widgetId: "getChinaRegionList",
		methodName: "execute",
		reginId: params.reginId,
	}).then((res) => res.content.result);
};

export const searchCompany = (params: { companyName: string }) => {
	return httpJsonp(commonFormUrl, {
		namespace: "searchEnterpriseName",
		widgetId: "searchEnterpriseName",
		methodName: "execute",
		keyword: params.companyName,
		limit: 10,
	}).then((res) => res.content.result);
};

export const getCompanyInfo = (params: { companyName: string }) => {
	return httpJsonp(commonFormUrl, {
		namespace: "searchEnterpriseInfo",
		widgetId: "searchEnterpriseInfo",
		methodName: "execute",
		name: params.companyName,
	}).then((res) => res.content.result);
};

export const getMoreInfoConfig = (params: { origin: string | number }) => {
	return getData("/potential/queryFormConfiguration.do", { origin: params.origin }).then((res) => res.values);
};
